package cs342.uwosh.edu.ContactsApp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cs342.uwosh.edu.ContactsApp.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ContactsActivity extends Activity {
    /** Called when the activity is first created. */
	private ListView listView;
	private ContactsDataSource db;
	private List<Contact> contacts;
	private Contact[] contactArray;
	static int theme = 0;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	switch(theme)
    	{
    		case 0:
    			this.setTheme(R.style.myDark);
    			break;
    		case 1:
    			this.setTheme(R.style.myLight);
    			break;
    		case 2:
    			this.setTheme(R.style.myMatrix);
    			break;
    	}
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayAdapter<Contact> arrayAdapter;// = new ArrayAdapter();
        listView =  (ListView)findViewById(R.id.listView1);
        
        //List<Contact> contacts = new ArrayList<Contact>();
        db = new ContactsDataSource(this);
        db.open();
        contacts = db.getAllContacts();
        db.close();
		if (contacts.size() > 0) {
			contactArray = new Contact[1];
			contactArray = contacts.toArray(contactArray);
			Arrays.sort(contactArray);

			arrayAdapter = new ArrayAdapter<Contact>(this,
					R.layout.contact_list, contactArray);

			listView.setOnItemClickListener(new OnItemClickListener() {
				
				
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Contact contact = contactArray[(int) id];
					DisplayContact.id = contact.getID();
					Intent i = new Intent(ContactsActivity.this,
							DisplayContact.class);
					ContactsActivity.this.startActivity(i);
				}
			});

			listView.setAdapter(arrayAdapter);
		}
                
    }
    
    
    public void add(View view) {
    	Intent i = new Intent(this, AddContact.class);
    	startActivity(i);
    }
    
    
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
    		case R.id.addOptionsMenu:
    			this.add(getCurrentFocus());
    			break;
    		case R.id.searchOptionsMenu:
    			this.search(getCurrentFocus());
    			break;
    		case R.id.settingsOptionsMenu:
    			this.settings(getCurrentFocus());
    			break;
    	}
    	return true;
    }
    
    public void settings(View view) {
    	Intent i = new Intent(ContactsActivity.this, SettingsActivity.class);
    	startActivity(i);
    }
    
    public void search(View view) {
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Search");
        alert.setMessage("Search for contacts");

        // Set an EditText view to get user input 
		final EditText input = new EditText(this);
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				SearchActivity.searchString = input.getText().toString();
				
				Intent i = new Intent(ContactsActivity.this, SearchActivity.class);
		    	startActivity(i);
				
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// Canceled.
					}
				});
		alert.show();
    }
}